<template>
  <div id="centreRight3">
    <div class="bg-color-black">
       <div class="titles">
          <span>近30天事件统计</span>
        </div>
        <div class="tabs">
          <div class="tabsItem"
           :class="{active: type == 0}"
           @click="changeLineData(0)"
          >全部</div>
          <div class="tabsItem"  :class="{active: type == 1}" @click="changeLineData(1)">健康</div>
          <div class="tabsItem" :class="{active: type == 2}" @click="changeLineData(2)">安全</div>
        </div>
        <div class="">
            <BottomRightChart :cdata="cdata"/>
        </div>
    </div>
  </div>
</template>

<script>
import BottomRightChart from "@/components/echart/bottom/bottomRightChart";
import { mapState } from 'vuex'; // 引入mapState
export default {
  data() {
    return {
      cdata: {
        barData: [],
        category: []
      },
      type: 0
    }
  },
  components: {
    BottomRightChart
  },
  computed: {
    ...mapState({
      cityId: state => state.system.cityId, // 从Vuex Store中获取cityId
    }),
  },
  mounted() {
      this.getLineData();
      this.$watch('cityId', (newVal, oldVal) => { // 监听cityId的变化
        if (newVal !== oldVal) {
          this.getLineData(); // 当cityId变化时重新请求数据
        }
      });
  },
  methods: {
    changeLineData(type) {
       this.type = type;
       this.getLineData();

    },
    getLineData() {
      this.$api.lineData({
        type: this.type, // 0全部，1健康，2安全
        region_id: this.cityId, 
      }).then(res => {
          if(res.code == 0) {
              // console.log('res', res.data)
              // 将返回的对象数据ageGroup进行键值对交换，然后赋值给barData和category
              const data = res.data;
              this.cdata.category = Object.keys(data);
              this.cdata.barData = Object.values(data);

          }
      })
    },
  }
};
</script>

<style lang="scss">
#centreRight3 {
  width: 4.9625rem;
  height: 3.875rem;
  box-sizing: border-box;
  margin-right: 0.125rem;
  // border: 1px solid red;
  .bg-color-black {
    width: 4.9625rem;
  }
    .tabs {
    display: flex;
    justify-content: flex-end;
    margin-top: 0.125rem;
    margin-right: 0.125rem;
    z-index: 999;
    position: relative;
    .tabsItem {
      width: 0.6625rem;
      height: 0.275rem;
      line-height: 0.275rem;
      border: 1px solid #11FFD7;
      color: #11FFD7;
      font-size: 0.15rem;
      text-align: center;
      margin-left: 0.125rem;
      font-weight: bold;
      cursor: pointer;
    }
    .active {
       background: #11FFD7;
       color: #172140!important;
       border: none!important;
    }
  }
  
  
}
</style>